#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCryptographicHash>
#include <QDebug>
#include "sqlcontroller.h"
#include <QMessageBox>
#include "usermanagement.h"
#include "userhome.h"
#include "maincontroller.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("用户登录界面");
    //添加单选框
    group = new QButtonGroup(this);
    group->addButton(ui->radioButton_user);
    group->addButton(ui->radioButton_manager);
    //初始化窗口转换类
    MainController::getMainController()->initMainWindow(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_btn_login_clicked()
{
    QString strUsername=ui->lineEdit_username->text();
    QString strPassword=ui->lineEdit_password->text();

    //登录
   bool ret = SqlController::getSqlController()->goLogin(strUsername,strPassword,role);

    if(ret)
    {
//        qDebug()<<role;
        QMessageBox::information(this,"登录提示：","登录成功！");
        if(role == 1){
            //判断是管理员，调用管理窗口
            MainController::getMainController()->showUserManagement();
            MainController::getMainController()->hideMainWindow();
        } else {
            //判断是用户，调用用户界面

            MainController::getMainController()->hideMainWindow();
            MainController::getMainController()->showUserHome();
        }

    }
    else
    {
        QMessageBox::information(this,"登录提示：","登录失败！");
    }
}

void MainWindow::on_radioButton_user_clicked()
{
    role = 0;
}


void MainWindow::on_radioButton_manager_clicked()
{
    role = 1;
}

void MainWindow::on_btn_quit_clicked()
{
    this->close();
}

